<script setup lang="ts">
import { requiredCheck } from "@/utils/verify";
import { ref } from "vue";
import { addData, editData, editPri } from "@/api/modules/resource/service";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增服务",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ElMessage } from "element-plus";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    account: [...requiredCheck("请输入用户名")],
    password: [...requiredCheck("请输入密码")]
  },
  defaultData: {
    role: "1"
  }
});

async function submitCb() {
  await updateFormRef.value.validate();
  if (props.rowData.srvid != 5) {
    let originData = props.rowData.originData;
    let obj = JSON.parse(originData[5]);
    if (obj.account?.length) {
      obj.account[0].user = updateFormData.account;
      obj.account[0].password = updateFormData.password;
    }
    let sendData = {
      id: props.rowData?.id,
      text: obj
    };
    let priMap = new Map([
      ["低", "1"],
      ["中", "2"],
      ["高", "3"]
    ]);
    await editPri({
      id: props.rowData?.id,
      pri: priMap.get(updateFormData.grade)
    });
    await editData(sendData);
    ElMessage.success("操作成功");
  }
  console.log(props.rowData);
  showHandle.value = false;
  emits("success");
}
</script>

<template>
  <div
    class="update-form mg-r-sm"
    style="display: inline-block"
    @click="
      openHandle({
        row: {
          ...rowData
        },
        id: props.rowData?.id
      })
    "
  >
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog @confirm="submitCb()" width="460px" :title="title" v-model="showHandle">
    <div class="details">
      <div class="details-main">
        <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="120px" label-suffix="：">
          <el-form-item label="用户名" prop="account">
            <el-input
              :disabled="rowData.srvid == 5"
              style="width: 240px"
              v-model="updateFormData.account"
              auto-complete="off"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              :disabled="rowData.srvid == 5"
              style="width: 240px"
              v-model="updateFormData.password"
              auto-complete="off"
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item label="危险等级" prop="grade">
            <el-select style="width: 240px" v-model="updateFormData.grade" auto-complete="off" placeholder="请选择危险等级">
              <el-option label="高" value="高"></el-option>
              <el-option label="中" value="中"></el-option>
              <el-option label="低" value="低"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="端口" prop="port">
            <el-input
              disabled
              style="width: 240px"
              v-model="updateFormData.port"
              auto-complete="off"
            />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped>
.update-form{
  display: inline-flex !important;
  align-items: center;
}
</style>
